package org.example.studentsdemo.teachers.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.example.studentsdemo.student.entity.Students;
import org.example.studentsdemo.student.mapper.StudentMapper;
import org.example.studentsdemo.teachers.DTO.addStudentsToTeacherDTO;
import org.example.studentsdemo.teachers.entity.teachers;
import org.example.studentsdemo.teachers.mapper.teachersMapper;
import org.example.studentsdemo.teachers.service.teachersService;
import org.example.studentsdemo.utils.HttpResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Function;

@Service
public class teachersServiceImpl implements teachersService {

    @Autowired
    private teachersMapper teacherMapper;
    @Autowired
    private StudentMapper studentMapper;
    /**
     * @param entityList
     * @param batchSize
     * @return
     */
    @Override
    public boolean saveBatch(Collection<teachers> entityList, int batchSize) {
        return false;
    }

    /**
     * @param entityList
     * @param batchSize
     * @return
     */
    @Override
    public boolean saveOrUpdateBatch(Collection<teachers> entityList, int batchSize) {
        return false;
    }

    /**
     * @param entityList
     * @param batchSize
     * @return
     */
    @Override
    public boolean updateBatchById(Collection<teachers> entityList, int batchSize) {
        return false;
    }

    /**
     * @param entity
     * @return
     */
    @Override
    public boolean saveOrUpdate(teachers entity) {
        return false;
    }

    /**
     * @param queryWrapper
     * @param throwEx
     * @return
     */
    @Override
    public teachers getOne(Wrapper<teachers> queryWrapper, boolean throwEx) {
        return null;
    }

    /**
     * @param queryWrapper
     * @param throwEx
     * @return
     */
    @Override
    public Optional<teachers> getOneOpt(Wrapper<teachers> queryWrapper, boolean throwEx) {
        return Optional.empty();
    }

    /**
     * @param queryWrapper
     * @return
     */
    @Override
    public Map<String, Object> getMap(Wrapper<teachers> queryWrapper) {
        return Map.of();
    }

    /**
     * @param queryWrapper
     * @param mapper
     * @param <V>
     * @return
     */
    @Override
    public <V> V getObj(Wrapper<teachers> queryWrapper, Function<? super Object, V> mapper) {
        return null;
    }

    /**
     * @return
     */
    @Override
    public BaseMapper<teachers> getBaseMapper() {
        return null;
    }

    /**
     * @return
     */
    @Override
    public Class<teachers> getEntityClass() {
        return null;
    }

    /**
     * @return
     */
    @Override
    public  ResponseEntity<HttpResult<List<teachers>>>  getTeacherList() {

        List<teachers> list = teacherMapper.selectList(new QueryWrapper<>());
        List<teachers> cc =new ArrayList<>();
        for (teachers teachers : list) {
          List<Students>  students =  studentMapper.selectList(new LambdaQueryWrapper<Students>().eq(Students::getTeacherId,teachers.getId()));
            teachers.setStudents(students);
            cc.add(teachers);
        }

        return new ResponseEntity<>(HttpResult.success(cc), HttpStatus.OK);
    }

    /**
     * @return
     */
    @Override
    public ResponseEntity<HttpResult<teachers>> addTeacher(teachers teacher) {
       teacherMapper.insert(teacher);
         return new ResponseEntity<>(HttpResult.success(teacher), HttpStatus.OK);
    }

    /**
     * @return
     */
    @Override
    public ResponseEntity<HttpResult<teachers>> updateTeacher(teachers teacher) {
       teacherMapper.updateById(teacher);
       return new ResponseEntity<>(HttpResult.success(teacher), HttpStatus.OK);
    }

    /**
     * @param id
     * @return
     */
    @Override
    public ResponseEntity<HttpResult<Boolean>> deleteTeacher(Long id) {
      int teacherState  =   teacherMapper.deleteById(id);
      if (teacherState > 0){
          return new ResponseEntity<>(HttpResult.success(true), HttpStatus.OK);
      }else{
          return new ResponseEntity<>(HttpResult.success(false), HttpStatus.OK);

      }

    }

    /**
     * @return
     */
    @Override
    public ResponseEntity<HttpResult<teachers>> getTeacherById(Long id) {

        teachers teacher = teacherMapper.selectById(id);
        List<Students>  students =  studentMapper.selectList(new LambdaQueryWrapper<Students>().eq(Students::getTeacherId,teacher.getId()));
        teacher.setStudents(students);

        return new ResponseEntity<>(HttpResult.success(teacher), HttpStatus.OK);
    }

    /**
     * @param addStudentsToTeacherDTO
     * @return
     */
    @Override
    public ResponseEntity<HttpResult<Boolean>> addStudentsToTeacherDTO(addStudentsToTeacherDTO addStudentsToTeacherDTO) {
     List<Integer> students =    addStudentsToTeacherDTO.getStudentIds();
    int teacherId =   addStudentsToTeacherDTO.getTeacherId();
    Boolean success = true;
            try{
                for (Integer student : students) {
                    Students students1= studentMapper.selectById(student);
                    students1.setTeacherId(teacherId);
                    studentMapper.updateById(students1);
                }
                return new ResponseEntity<>(HttpResult.success(success), HttpStatus.OK);
            }catch (Exception e){
                success = false;
                return new ResponseEntity<>(HttpResult.success(success), HttpStatus.OK);

            }

    }
}
